/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

/**
 *
 * @author steely
 */
public class Shipyard {

    // corvettes base build time in seconds

    private static final int BASE_VETT_BUILD_TIME = 11070;

    private int level;
    private int amountToBuild;
    private int totalBuildTime;
    private int vettBuildTime;

    public Shipyard(int level) {
        this.level = level;
        amountToBuild = 0;
        totalBuildTime = 0;
        vettBuildTime = (int) (Math.round(BASE_VETT_BUILD_TIME / Math.pow(1.2, (level - 1))));
    }

    public int getLevel() {
        return level;
    }

    public int getAmountToBuild() {
        return amountToBuild;
    }

    public int getTotalBuildTime() {
        return totalBuildTime;
    }

    public int getVettBuildTime() {
        return vettBuildTime;
    }

    public void buildAnother() {
        amountToBuild++;
        totalBuildTime += vettBuildTime;
    }

    public void printInfo() {
        System.out.println("Build " + amountToBuild + " corvettes with your level " + level + " shipyard.");
        System.out.println("Build time: " + convertTime(totalBuildTime));
    }

    private String convertTime(int inSeconds) {
        int days = (int) (Math.floor(inSeconds / 86400));
        int hours = (int) (Math.floor(inSeconds / 3600) % 24);
        int minutes = (int) (Math.floor(inSeconds / 60) % 60);
        int seconds = inSeconds % 60;
        String formattedTime = "";
        int var = 0;

        if (days > 0) var++;
        if (hours > 0) var++;
        if (minutes > 0) var++;
        if (seconds > 0) var++;
        if (var == 2) {
            if (days > 0) {
                var--;
                formattedTime = formattedTime + days + " " + checkPlural("day", days) + " and ";
            }
            if (hours > 0) {
                var--;
                formattedTime = var == 1 ? formattedTime + hours + " " + checkPlural("hour", hours) + " and " : formattedTime + hours + checkPlural("hour", hours) + ".";
            }
            if (minutes > 0) {
                var--;
                formattedTime = var == 1 ? formattedTime + minutes + " " + checkPlural("minute", minutes) + " and " : formattedTime + minutes + checkPlural("minute", minutes) + ".";
            }
            if (seconds > 0) {
                formattedTime = formattedTime + seconds + " " + checkPlural("second", seconds) + ".";
            }
        } else {
            if (days > 0) {
                var--;
                formattedTime = formattedTime + days + " " + checkPlural("day", days) + checkTimeline(var);
            }
            if (hours > 0) {
                var--;
                formattedTime = formattedTime + hours + " " + checkPlural("hour", hours) + checkTimeline(var);
            }
            if (minutes > 0) {
                var--;
                formattedTime = formattedTime + minutes + " " + checkPlural("minute", minutes) + checkTimeline(var);
            }
            if (seconds > 0) {
                formattedTime = formattedTime + seconds + " " + checkPlural("second", seconds) + ".";
            }
        }

        return formattedTime;
    }

    // checks for what puntuation is needed
    private String checkTimeline(int var) {
        String addition;
        if (var == 0) {
            addition = ".";
        } else if (var == 1) {
            addition = ", and ";
        } else {
            addition = ", ";
        }
        return addition;
    }
    
    private String checkPlural(String measurement, int amount) {
        if(measurement.contains("day")) {
            return amount == 1 ? "day" : "days";
        } else if(measurement.contains("hour")) {
            return amount == 1 ? "hour" : "hours";
        } else if(measurement.contains("minute")) {
            return amount == 1 ? "minute" : "minutes";
        } else if(measurement.contains("second")) {
            return amount == 1 ? "second" : "seconds";
        } else {
            return "error";
        }
    }
}
